<?php
namespace Swiftx\DateTime;
use Swiftx\Base\Object;

/**
 +---------------------------------------------------------------------------------------------------------------
 * 时间类
 +---------------------------------------------------------------------------------------------------------------
 * @author		Hismer <odaytudio@gmail.com>
 * @since		2014-11-11
 * @copyright	Copyright (c) 2014-2015 ShopBCO Inc.
 +---------------------------------------------------------------------------------------------------------------
 */
class Time extends Object {
	
	protected $_stamp;		// 戳
	protected $_year;		// 年
	protected $_month;		// 月
	protected $_day;		// 日
	protected $_week;		// 星期
	protected $_hour;		// 时
	protected $_minute;		// 分
	protected $_second;		// 秒
	
	/**
	 +----------------------------------------------------------
	 * 时间戳
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
	protected function _getStamp(){
		if(empty($this->_stamp))
			$this->_SyncStamp();
		return $this->_stamp;
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间戳
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setStamp($value){
		$this->_stamp = $value;
		$this->_SyncAttribute();
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间:年
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
	protected function _getYear(){
		return $this->_year;
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间:年
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setYear($value){
		$this->_stamp = null;
		$this->_year = $value;
	}

	/**
	 +----------------------------------------------------------
	 * 时间:月
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
	protected function _getMonth(){
		return $this->_month < 10 ? '0'.$this->_month:$this->_month;
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间:月
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setMonth($value){
		$this->_stamp = null;
		$this->_month = $value;

	}

	/**
	 +----------------------------------------------------------
	 * 时间:日
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
	protected function _getDay(){
		return $this->_day < 10 ? '0'.$this->_day:$this->_day;
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间:日
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setDay($value){
		$this->_stamp = null;
		$this->_day = $value;
	}

    public function moveDay($value){
        $this->_stamp += 24*60*60*$value;
        $this->_SyncAttribute();
    }

	/**
	 +----------------------------------------------------------
	 * 时间:星期
	 +----------------------------------------------------------
	 * @return int
	 +----------------------------------------------------------
	 */
	protected function _getWeek(){
		return $this->_week;
	}

	/**
	 +----------------------------------------------------------
	 * 时间:时
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
	protected function _getHour(){
		return $this->_hour;
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间:时
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setHour($value){
		$this->_stamp = null;
		$this->_hour = $value;
	}

	/**
	 +----------------------------------------------------------
	 * 时间:分
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
	protected function _getMinute(){
		return $this->_minute;
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间:时
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setMinute($value){
		$this->_stamp = null;
		$this->_minute = $value;
	}

	/**
	 +----------------------------------------------------------
	 * 时间:秒
	 +----------------------------------------------------------
	 * @return bool
	 +----------------------------------------------------------
	 */
	protected function _getSecond(){
		return $this->_second;
	}
	
	/**
	 +----------------------------------------------------------
	 * 时间:秒
	 +----------------------------------------------------------
	 * @param int $value
	 +----------------------------------------------------------
	 */
	protected function _setSecond($value){
		$this->_stamp = null;
		$this->_second = $value;
	}

	/**
	 +----------------------------------------------------------
	 * 构造方法,时间初始化
	 +----------------------------------------------------------
	 * @param $data	可选，时间的数据
	 +----------------------------------------------------------
	 */
	public function __construct($data = null) {
		if(empty($data)){
			$this->_stamp = \time();
			$this->_SyncAttribute();
		}else if(is_int($data)){
			$this->_stamp = $data;
			$this->_SyncAttribute();
		}else if(is_array($data)){
			$this->_stamp = \mktime($data['hour'], $data['minute'], $data['second'],$data['month'], $data['day'], $data['year']);
			$this->_SyncAttribute();
		}else{
			
		}
		
	}
	
	/**
	 +----------------------------------------------------------
	 * 构造方法,时间初始化
	 +----------------------------------------------------------
	 * @return string	Y-m-d H:i:s 标准格式输出时间
	 +----------------------------------------------------------
	 */
	public function __toString(){
		return \date('Y-m-d H:i:s', $this->_stamp);
	}
	
	public function Format($string){
		return \date($string, $this->_stamp);
	}
	
	/**
	 +----------------------------------------------------------
	 * 从时间属性同步到时间戳
	 +----------------------------------------------------------
	 */
	protected function _SyncStamp(){
		$this->_stamp = \mktime(
			$this->_hour, $this->_minute, $this->_second,
			$this->_month, $this->_day, $this->_year
		);
	}
	
	/**
	 +----------------------------------------------------------
	 * 从时间戳同步到时间属性
	 +----------------------------------------------------------
	 */
	protected function _SyncAttribute(){
		$date = \getdate($this->_stamp); 
		$this->_year = $date['year'];
		$this->_month = $date['mon'];
		$this->_day = $date['mday'];
		$this->_week = $date['wday'];
		$this->_hour = $date['hours'];
		$this->_minute = $date['minutes'];
		$this->_second = $date['seconds'];
	}

}